package com.xxl.job.executor.service.jobhandler;

import com.ruoyi.bizsys.domain.LoanOrder;
import com.ruoyi.bizsys.domain.SmsTask;
import com.ruoyi.bizsys.service.ILoanOrderService;
import com.ruoyi.bizsys.vo.LoanOrderVo;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.sms.service.ISmsService;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: ygw
 * @Date: 2022/03/15/2:18 下午
 * @Description: 放款致歉短信定时任务类
 */
@Component
public class ApologizeSmsJob {

    public static final String SMS_TYEP_CNCL_RE = "temp_wdrw_cncl_re";
    public static final String SMS_TYEP_FAIL_TE = "temp_wdrw_fail_re";
    public static final String SMS_TYEP_PROC_SRY = "temp_wdrw_proc_sry";
    public static final String SMS_TYEP = "apology";
    public static final String REMARK_ACCOUNT = "Account number is invalid";
    public static final String REMARK_CANNOT = "Cannot resolve account";
    private static final String APOLOGIZE_TYPE_LOAN_FAILURE = "1";
    private static final String APOLOGIZE_TYPE_LOAN_PENDING = "2";

    @Autowired
    private ILoanOrderService loanOrderService;

    @Autowired
    private ISmsService smsService;

    /**
     * 致歉短信任务
     */
    @XxlJob("apologizeSmsJobHandler")
    public void apologize() {
        XxlJobHelper.log("致歉短信任务 Start >>>>>>>>>>");

        String params = XxlJobHelper.getJobParam();
        if (StringUtils.isBlank(params)) {
            XxlJobHelper.log("致歉短信任务 End >>>>>>>>>> 参数为空");
            return;
        }

        String[] taskArray = params.split(",");
        for (String task : taskArray) {
            if (StringUtils.equals(APOLOGIZE_TYPE_LOAN_FAILURE, task)) {
                try {
                    loanFailureApologize();
                } catch (Exception e) {
                    XxlJobHelper.log("loanFailure 致歉短信任务 执行异常");
                    XxlJobHelper.log(e);
                    XxlJobHelper.handleFail("loanFailure 致歉短信任务 执行异常");
                }
            }

            if (StringUtils.equals(APOLOGIZE_TYPE_LOAN_PENDING, task)) {
                try {
                    loanPendingApologize();
                } catch (Exception e) {
                    XxlJobHelper.log("loanPending 致歉短信任务 执行异常");
                    XxlJobHelper.log(e);
                    XxlJobHelper.handleFail("loanPending 致歉短信任务 执行异常");
                }
            }
        }

        XxlJobHelper.log("致歉短信任务 End >>>>>>>>>>");
    }

    /**
     * 放款失败致歉短信
     */
    public void loanFailureApologize() {
        XxlJobHelper.log("loanFailure 致歉短信任务 Start >>>>>>>>>>");

        List<LoanOrder> loanOrders = new ArrayList<>();
        List<SmsTask> smsTasks = new ArrayList<>();

        List<LoanOrderVo> loanOrderVo = loanOrderService.selectLoanOrder();
        if (!loanOrderVo.isEmpty()) {
            for (int i = 0; i < loanOrderVo.size(); i++) {
                SmsTask smsTask = new SmsTask();
                LoanOrder loanOrder = new LoanOrder();
                if (StringUtils.equalsAny(loanOrderVo.get(i).getRemark(), REMARK_ACCOUNT, REMARK_CANNOT)) {
                    smsTask.setSmsType(SMS_TYEP_CNCL_RE);
                    smsTask.setNoticeType(SMS_TYEP);
                    smsTask.setPhone(loanOrderVo.get(i).getPhone());
                    smsTask.setContent("Dear " + loanOrderVo.get(i).getLastName() + ", Sorry, your " + loanOrderVo.get(i).getLoanAmt() + "N withdrawal request has been canceled due to the receiving account being invalid. Please add a different account and resubmit the withdrawal application.");
                    loanOrder.setSendSms(1);
                    loanOrder.setId(loanOrderVo.get(i).getId());
                } else {
                    smsTask.setSmsType(SMS_TYEP_FAIL_TE);
                    smsTask.setNoticeType(SMS_TYEP);
                    smsTask.setPhone(loanOrderVo.get(i).getPhone());
                    smsTask.setContent("Dear " + loanOrderVo.get(i).getLastName() + ", We sincerely apologize, your " + loanOrderVo.get(i).getLoanAmt() + "N withdrawal request has been canceled due to a network issue. Please resubmit the withdrawal application.");
                    loanOrder.setSendSms(1);
                    loanOrder.setId(loanOrderVo.get(i).getId());
                }
                //存入集合
                smsTasks.add(smsTask);
                loanOrders.add(loanOrder);
            }
            XxlJobHelper.log("开始发送 loanFailure 致歉短信");
            smsService.sendBatchSms(smsTasks);

            XxlJobHelper.log("开始修改 send_sms 状态");
            loanOrderService.updateBatch(loanOrders);
        }
        XxlJobHelper.log("loanFailure 致歉短信发送任务共 {} 条", loanOrderVo.size());

        XxlJobHelper.log("loanFailure 致歉短信任务 End >>>>>>>>>>");
    }

    /**
     * 放款等待致歉短信
     */
    public void loanPendingApologize() {
        XxlJobHelper.log("loanPending 致歉短信任务 Start >>>>>>>>>>");

        List<SmsTask> smsTasks = new ArrayList<>();
        List<LoanOrder> loanOrders = new ArrayList<>();

        //time-create_time>=30min
        List<LoanOrderVo> loanOrderVo = loanOrderService.selectLoanOrderPending();
        if (!loanOrderVo.isEmpty()) {
            for (int i = 0; i < loanOrderVo.size(); i++) {
                LoanOrder loanOrder = new LoanOrder();
                SmsTask smsTask = new SmsTask();
                smsTask.setSmsType(SMS_TYEP_PROC_SRY);
                smsTask.setNoticeType(SMS_TYEP);
                smsTask.setPhone(loanOrderVo.get(i).getPhone());
                smsTask.setContent("Dear " + loanOrderVo.get(i).getLastName() + ", we sincerely apologize for the delay in your withdrawal! Your " + loanOrderVo.get(i).getLoanAmt() + "N withdrawal request will be processed within 24 hrs! If you have any questions, please contact Linkcredit WhatsApp at +2348050995364 for more!");
                loanOrder.setSendSms(1);
                loanOrder.setId(loanOrderVo.get(i).getId());
                smsTasks.add(smsTask);
                loanOrders.add(loanOrder);
            }
            XxlJobHelper.log("开始发送 loanPending 致歉短信");
            smsService.sendBatchSms(smsTasks);

            XxlJobHelper.log("开始修改 send_sms 状态");
            loanOrderService.updateBatch(loanOrders);
        }

        XxlJobHelper.log("本次 loanPending 致歉短信发送任务共 {} 条", loanOrderVo.size());

        XxlJobHelper.log("loanPending 致歉短信任务 End >>>>>>>>>>");
    }

}
